package com.example.administrator.szb.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.bean.ResultBean;
import com.example.administrator.szb.config.FragmentsFlag;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.GetJsonDataUtil;
import com.example.administrator.szb.util.Toasts;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by D N on 2017/10/10.
 * 意见反馈/取消订单原因
 */

public class YJFKActivity extends BaseActivity {
    @Bind(R.id.yjfk_edit_jy)
    EditText yjfkEditJy;
    @Bind(R.id.yjfk_ll_jy)
    LinearLayout yjfkLlJy;
    @Bind(R.id.yjfk_button_tj)
    Button yjfkButtonTj;

    String url = "";
    int indent_id = 0;
    int code = 0;
    String message = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yjfk_activity);
        ButterKnife.bind(this);
        hasIntent();
        SampleApplicationLike.getActivitiesInstance().add(this);
    }

    private void hasIntent() {
        code = getIntent().getIntExtra("who",-1);
        if (code == FragmentsFlag.Activity_XMXQ.ordinal()){
            message = "请输入取消订单";
            initToolbar(R.id.toolbar_include_yjfk,"取消订单");
            yjfkEditJy.setHint("请输入取消订单原因");
            indent_id = getIntent().getIntExtra("id_dd",-1);
            url = URLAddress.URLBASE + URLAddress.URL_QXDD;
        }else if(code == FragmentsFlag.Activity_SZ.ordinal()){
            message = "请输入意见反馈";
            initToolbar(R.id.toolbar_include_yjfk,"意见反馈");
            url = URLAddress.URLBASE + URLAddress.URL_YJFK;
        }else if(code == FragmentsFlag.Activity_XMXQ_JJJD.ordinal()){
            message = "请输入拒绝接单的原因";
            initToolbar(R.id.toolbar_include_yjfk,"拒绝订单");
            yjfkEditJy.setHint("请输入拒绝接单的原因");
            indent_id = getIntent().getIntExtra("id_dd",-1);
            url = URLAddress.URLBASE + URLAddress.URL_JJJD_NEW;
        }
    }

    @OnClick({ R.id.yjfk_ll_jy, R.id.yjfk_button_tj})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.yjfk_ll_jy:
                /**
                 * 弹出软键盘
                 */
                yjfkEditJy.requestFocus();
                InputMethodManager imm = (InputMethodManager) yjfkEditJy.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
                break;
            case R.id.yjfk_button_tj:
                if (!TextUtils.isEmpty(yjfkEditJy.getText().toString())){
                    doSubmit();
                }else {
                    DialogUtil.showToast(YJFKActivity.this,message);
                }

                break;
        }
    }

    private void doSubmit() {
        HashMap<String,String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        if (code == FragmentsFlag.Activity_SZ.ordinal()){
            map.put("users_id",SampleApplicationLike.getUserloginInstance().getId()+"");
            map.put("content",yjfkEditJy.getText().toString()+"");
        }else if(code == FragmentsFlag.Activity_XMXQ.ordinal()){
            map.put("indent_id",indent_id+"");
            map.put("reasons",yjfkEditJy.getText().toString()+"");
        }else if(code == FragmentsFlag.Activity_XMXQ_JJJD.ordinal()){
            map.put("indent_id",indent_id+"");
            map.put("cause",yjfkEditJy.getText().toString()+"");
            map.put("users_id",SampleApplicationLike.getUserloginInstance().getId()+"");
        }
        String json = GetJsonDataUtil.simpleMapToJsonStr(map);

        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, ResultBean.class, url, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ResultBean bean = (ResultBean) response;
                if (bean.getResult() == 1){
                    DialogUtil.showToast(YJFKActivity.this,"提交成功");
                    ProductDetailActivity.canRefresh=true;
                    ProductDetail2Activity.canRefresh=true;
                    finish();
                }else{
                    Toasts.show(YJFKActivity.this, "提交异常，客官再试一次吧...", Toast.LENGTH_SHORT);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toasts.show(YJFKActivity.this, ""+error.toString(), Toast.LENGTH_SHORT);
            }
        });
    }
}
